package com.yuke.cloud.service.tmc.job.database;


import com.yuke.cloud.service.tmc.util.BackUpDataBaseManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

@Component
@Slf4j
@EnableScheduling
public class BackUpDataBase {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");

    @Autowired
    private BackUpDataBaseManager backUpDataBaseManager;

    /**
     *在数据库直接配置了docker 定时任务，此段代码暂时用不到
     * 后期修改为能远程调用*.sh文件，调用数据库服务器的备份，需要安装ssh依赖
     */
    //@Scheduled(cron = "0 10 9 * * ?")  // 每天凌晨3点进行
    public void backupDatabase() {
        String format = dateFormat.format(System.currentTimeMillis());
        log.info("The time is now {}", format);
        backUpDataBaseManager.exportSql(format);
        try {
            backUpDataBaseManager.backup(format);
        }catch (Exception e){
            log.error("备份 数据库 出现异常 ", e);
        }

    }
}
